//
//  SCAllCityViewController.m
//  LastOne
//
//  Created by dllo on 15/11/8.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "SCAllCityViewController.h"
#import "SCWebViewController.h"
#import "SCDiscountCollectionViewCell.h"
#import "HTTPMethod.h"
#import <UIImageView+WebCache.h>
#import "NightHeader.h"
#import "GiFHUD.h"
#import "NSString+EmString.h"
#import "FitHeader.h"
#define URLString @"http://open.qyer.com/qyer/discount/tickets_freewalker?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=861138023945773&track_app_version=6.7&track_app_channel=360m&track_device_info=mx3&track_os=Android4.2.1&app_installtime=1443017336409&lat=38.883593&lon=121.54466&type=2&id=%@&count=10&page=%ld&product_type=1016,1018,1020&time=1&order=2"
@interface SCAllCityViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, retain)NSMutableArray *dataSource;
@property(nonatomic, retain)UICollectionView *collectionView;
@end

@implementation SCAllCityViewController

//-(void)netError:(NSNotification *)notification
//{
//    //加载时停止加载
//    [self.collectionView.footer endRefreshing];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netError:) name:@"net" object:nil];
    
    self.dataSource = [NSMutableArray array];
    _page = 1;
    [self getData];
    //加载页面
    [GiFHUD setGifWithImageName:@"loading.gif"];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(15 * FitWidth, 15 * FitHeight, 15 * FitWidth, 15 * FitHeight);
    flowLayout.minimumInteritemSpacing = 15 * FitWidth
;
    flowLayout.minimumLineSpacing = 15 * FitHeight;
    flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 15 * FitWidth * 3 - 5 * FitWidth) / 2.0, 180 * FitHeight);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView NightWithType:UIViewColorType2];
    [self.view addSubview:_collectionView];
    [self.collectionView registerClass:[SCDiscountCollectionViewCell class] forCellWithReuseIdentifier:@"reuseIdentifier"];
//    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        _page++;
//        [self initData];
//    }];
}

-(void)getData
{
    [GiFHUD show];
    [HTTPMethod getDataByString:[NSString stringWithFormat:URLString, _cityId, _page] BodyDic:nil WithDataBlock:^(id data) {
        [_dataSource addObjectsFromArray:[[data objectForKey:@"data"] objectForKey:@"list"]];
        [self.collectionView reloadData];
       // [self.collectionView.footer endRefreshing];
        [GiFHUD dismiss];
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (_dataSource.count == 0) {
        return 0;
    }
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SCDiscountCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.cornerRadius = 10;
    cell.layer.masksToBounds = YES;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[[_dataSource objectAtIndex:indexPath.row] objectForKey:@"photo"]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cell.nameLabel.text = [[_dataSource objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.priceLabel.text = [NSString stringWithFormat:@"%@", [NSString getStringByEmString:[[_dataSource objectAtIndex:indexPath.row] objectForKey:@"price"]]];
    cell.priceoffLabel.text = [[_dataSource objectAtIndex:indexPath.row] objectForKey:@"priceoff"];
    cell.nameLabel.textColor = [UIColor darkGrayColor];
    [cell NightWithType:UIViewColorTypeNormal];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SCWebViewController *webVC = [[SCWebViewController alloc] init];
    webVC.urlString = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_android&track_app_version=6.7&track_deviceid=861138023945773&ra_referer=sift_list&ra_arg=a5f6d7a82b858cd2f614524794f619b0663af25d", [[_dataSource objectAtIndex:indexPath.row] objectForKey:@"id"]];
    [self.navigationController pushViewController:webVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
